package agu.widget;

import agu.builtin.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatSpinner;

public class DropDownButton extends AppCompatSpinner
{
    int textColor = -1;
    float textSize = -1;
    String text = null;
    String textSeparator = null;
    List<Object> items;

    public DropDownButton(@NonNull Context context){
        super(context);
        init(context, null);
    }

    public DropDownButton(@NonNull Context context, @Nullable AttributeSet attrs){
        super(context, attrs);
        init(context, attrs);
    }

    public DropDownButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs){
        if(attrs != null){
            TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.DropDownButton);
            textColor = ta.getColor(R.styleable.DropDownButton_android_textColor, -1);
            textSize = ta.getDimension(R.styleable.DropDownButton_android_textSize, -1);
            text = ta.getString(R.styleable.DropDownButton_android_text);
            textSeparator = ta.getString(R.styleable.DropDownButton_textSeparator);
            ta.recycle();
        }

        if(!TextUtils.isEmpty(text)){
            items = Arrays.asList(text.split(Objects.toString(textSeparator, "\n")));
        }else{
            SpinnerAdapter adapter = getAdapter();
            int count = adapter == null? 0: adapter.getCount();
            if(count > 0){
                items = new ArrayList<>(count);
                for(int i = 0; i < count; i++) items.add(adapter.getItem(i));
            }
        }

        setAdapter(new LocalAdapter(context));
    }

    public void setItems(List<Object> items){
        this.items = items;
        requestLayout();
    }

    class LocalAdapter extends ArrayAdapter<Object>
    {
        public LocalAdapter(@NonNull Context context){
            super(context, android.R.layout.simple_spinner_item);
            setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        }

        @Override
        public Object getItem(int position){
            return items.get(position);
        }

        @Override
        public int getCount(){
            return items == null? 0: items.size();
        }

        @Override
        public View getView(int position, View view, ViewGroup parent){
            return fixView(super.getView(position, view, parent));
        }

        @Override
        public View getDropDownView(int position, View view, ViewGroup parent){
            return fixView(super.getDropDownView(position, view, parent));
        }

        View fixView(View view){
            if(view instanceof TextView){
                TextView textView = (TextView)view;
                textView.setGravity(getGravity());
                if(textSize != -1){
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
                }
                if(textColor != -1){
                    textView.setTextColor(textColor);
                }
            }
            return view;
        }
    }

}
